package cn.edu.zjicm.wordsnet_d.activity;

import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;
import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.activity.base.WordBaseActivity;
import cn.edu.zjicm.wordsnet_d.data.DataManager;
import cn.edu.zjicm.wordsnet_d.data.OneWord;
import cn.edu.zjicm.wordsnet_d.datapersistent.AppPreference;
import cn.edu.zjicm.wordsnet_d.datapersistent.FileManager;
import cn.edu.zjicm.wordsnet_d.db.WordFactory;
import cn.edu.zjicm.wordsnet_d.view.MyDialog;

import com.umeng.analytics.MobclickAgent;

public class ReviewActivity extends WordBaseActivity {
	public static void startActivity(Context context) {
		Intent i = new Intent(context, ReviewActivity.class);
		context.startActivity(i);
	}

	private Button reviewFamiliarWordsButton;
	private TextView startReviewFamiliarText;
	private Button reviewWordsButton;
	private TextView startReviewText;
	private Button studyNewWordsButton;
	private Button studyOldWordsButton;
	private TextView studyOldWordsText;

	private Button startSpellButton;
	private Button startHuanboButton;
	private TextView startHuanboText;
	private ImageView showRegularImg;
	private ViewFlipper studyFlipper;

	int huanboWordsNum = 0;

	// Handler loadWindowHandler = new LoadWindowHandler(this);

	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setVolumeControlStream(AudioManager.STREAM_MUSIC);

		setTitleBarStyle(TITLE_BAR_STYLE_BACK_BUTTON);
		setTitleText("加量学习");
		setContentView(R.layout.activity_review);

		findViews();
		initViews();
	}

	private void findViews() {
		reviewFamiliarWordsButton = (Button) findViewById(R.id.review_familiar_words_button);
		reviewWordsButton = (Button) findViewById(R.id.review_words_button);
		studyNewWordsButton = (Button) findViewById(R.id.study_new_words_button);
		startSpellButton = (Button) findViewById(R.id.start_spell_button);
		startHuanboButton = (Button) findViewById(R.id.start_huanbo_button);
		startHuanboText = (TextView) findViewById(R.id.start_huanbo_text);
		startReviewFamiliarText = (TextView) findViewById(R.id.start_review_familiar_text);
		startReviewText = (TextView) findViewById(R.id.start_review_text);

		studyOldWordsButton = (Button) findViewById(R.id.study_old_words_button);
		studyOldWordsText = (TextView) findViewById(R.id.study_old_word_text);
		showRegularImg = (ImageView) findViewById(R.id.showRegularImg);
		studyFlipper = (ViewFlipper) findViewById(R.id.study_flipper);
	}

	public void initViews() {
		reviewFamiliarWordsButton.setOnClickListener(clickListener);
		reviewWordsButton.setOnClickListener(clickListener);
		studyNewWordsButton.setOnClickListener(clickListener);
		startSpellButton.setOnClickListener(clickListener);
		startHuanboButton.setOnClickListener(clickListener);
		studyOldWordsButton.setOnClickListener(clickListener);
		showRegularImg.setOnClickListener(clickListener);
		initCountText();
	}

	void initCountText() {
		startReviewFamiliarText.setText("巩固学习" + AppPreference.getNewWordsFmDegree(this, 5) + "个熟词");
		startReviewText.setText("巩固最近学过的" + WordFactory.getInstance(this).getNumberOfNewWordOrLastForgetWords() + "个生词");

		int reviewCount = WordFactory.getInstance(this).getRealNumberOfWordsForTestOfDegree(OneWord.DEGREE_UNFAMILIAR);
		studyOldWordsText.setText("继续复习" + reviewCount + "个单词");
		if (reviewCount == 0)
			studyFlipper.setDisplayedChild(0);
		else
			studyFlipper.setDisplayedChild(1);

		huanboWordsNum = ExamHuanboActivity.initHuanboList(this);
		if (!FileManager.hasNotDownloadedVoicePack(this, true)) {
			startHuanboText.setText("听音、复读、跟读" + huanboWordsNum + "个较生单词");
		} else {
			startHuanboText.setText("语言包不完整，听音功能无法正常使用");
		}
	}

	OnClickListener clickListener = new OnClickListener() {

		@Override
		public void onClick(View view) {
			// TODO Auto-generated method stub
			if (view == reviewFamiliarWordsButton) {
				if (AppPreference.getNewWordsFmDegree(ReviewActivity.this, 5) == 0)
					Toast.makeText(ReviewActivity.this, "没有熟词需要复习", Toast.LENGTH_SHORT).show();
				else
					ExamReviewActivity.startActivity(ReviewActivity.this, OneWord.DEGREE_FAMILIAR);
			} else if (view == reviewWordsButton) {
				if (WordFactory.getInstance(ReviewActivity.this).getNumberOfNewWordOrLastForgetWords() == 0)
					Toast.makeText(ReviewActivity.this, "没有最近学过的单词需要复习", Toast.LENGTH_SHORT).show();
				else
					ExamReviewActivity.startActivity(ReviewActivity.this, OneWord.DEGREE_UNFAMILIAR);
			} else if (view == studyNewWordsButton) {
				int num = WordFactory.getInstance(ReviewActivity.this).getUnLearnedWordCount();
				if (num == 0) {
					Toast.makeText(ReviewActivity.this, "已完成当前词汇书的学习", Toast.LENGTH_SHORT).show();
				} else {
					final int addWordNum;
					if (num > 20)
						addWordNum = 20;
					else
						addWordNum = num;
					showAddNewWordsDialog(addWordNum);
				}
			} else if (view == studyOldWordsButton) {
				ExamRunActivity.startActivity(ReviewActivity.this, OneWord.DEGREE_UNFAMILIAR, ExamRunActivity.REVIEW_MODE);
			} else if (view == startSpellButton) {
				ExamSpellActivity.startActivity(ReviewActivity.this);
			} else if (view == startHuanboButton) {
				if (FileManager.hasNotDownloadedVoicePack(ReviewActivity.this, true))
					Toast.makeText(ReviewActivity.this, "有部分音频文件没有下载，可能会影响听音，建议去设置页面下载", Toast.LENGTH_LONG).show();
				else
					ExamHuanboActivity.startActivity(ReviewActivity.this);
			} else if (view == showRegularImg) {
				showRegularDialog(view);
			}
		}
	};

	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
		initCountText();
	}

	private void showAddNewWordsDialog(final int addWordNum) {
		View view = LayoutInflater.from(this).inflate(R.layout.view_dialog_normal, null);
		TextView content = (TextView) view.findViewById(R.id.dialog_content);
		Button continueBtn = (Button) view.findViewById(R.id.dialog_btn_ok);
		Button cancel = (Button) view.findViewById(R.id.dialog_btn_cancel);
		content.setText("是否加学" + addWordNum + "个生词？");
		continueBtn.setText("继续");
		cancel.setText("取消");

		final MyDialog mDialog = new MyDialog(this, view, R.style.mydialog, false);
		mDialog.setCanceledOnTouchOutside(true);
		mDialog.show();
		continueBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				DataManager.getInstance().addExtraWordsForStudy(ReviewActivity.this, addWordNum);
				ExamRunActivity.startActivity(ReviewActivity.this, OneWord.DEGREE_UNFAMILIAR, ExamRunActivity.STUDY_MODE);
				mDialog.dismiss();
				finish();

			}
		});
		cancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mDialog.dismiss();
			}
		});
	}

	private void showRegularDialog(View parent) {
		View view = LayoutInflater.from(this).inflate(R.layout.view_show_regular_dialog, null);
		TextView textView = (TextView) view.findViewById(R.id.regular_tv);
		String tv1 = getResources().getString(R.string.review_regular);
		textView.setText(tv1);
		RelativeLayout closeRegular = (RelativeLayout) view.findViewById(R.id.closeRegularBtn);
		final MyDialog dialog = new MyDialog(this, view, R.style.mydialog, false);
		closeRegular.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.cancel();
			}
		});

		dialog.setCanceledOnTouchOutside(true);
		dialog.show();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPause(this);
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}
}
